package com.example.teamtribe.main.club.clubintroduce.media.photo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.event.Refresh;
import com.luck.picture.lib.entity.LocalMedia;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.List;

/**
 * Created by lenovo on 2018/6/17.
 */

public class AddPhotoPresenter implements AddPhotoContract.Presenter{
    private AddPhotoContract.View view;
    private PhotoModel model;

    public AddPhotoPresenter(AddPhotoContract.View view) {
        this.view = view;
        model =new PhotoModel();
    }


    @Override
    public void addPhotos(final String token,final List<LocalMedia> localMedias,final int teamId,final int albumId) {
        if(localMedias==null||localMedias.isEmpty()){
            Log.d("zsm", "null ");
            view.showToast("您还未选择照片");
            return;
        }
        view.showProgress();
        for(int i=0;i<localMedias.size();i++){
            String photoPath=localMedias.get(i).getPath();
            File file = new File(photoPath);
            final int j=i;
            FileModel.getInstance().upload(token, file, 2, new Back() {
                @Override
                public void success(@Nullable Bundle bundle) {
                    Log.d("zsm", "photo-upload-success: ");
                    addPhoto(token, teamId, bundle.getString("fileName"),albumId);
                    if(j==localMedias.size()-1){
                        EventBus.getDefault().postSticky(new Refresh(Refresh.PHOTOACTIVITY));
                        view.hideProgress();
                        view.next();
                    }
                }

                @Override
                public void failure(String meg) {
                    view.hideProgress();
                    view.showToast(meg);
                }
            });

        }

    }
    private void addPhoto(String token, int teamId, String filename, int albumId){
        model.addPhoto(token, teamId, filename, albumId, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                Log.d("zsm", "photo-add-success: ");
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }

    private void upload(final String token,final int teamId,final String photoPath,final int albumId) {
        File file = new File(photoPath);
        FileModel.getInstance().upload(token, file, 2, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                addPhoto(token, teamId, bundle.getString("fileName"),albumId);
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }
}
